package cn.tedu.wx.vo;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @author Kiven
 * @Date 2021-06-20
 * 该类为Access_token存储bean
 */
@Data
public class AccessTokenBean implements Serializable {
    /**
     *  固定值.
     */
    private String grant_type = "client_credential";
    /**
     * 开发者APPID
     */
    private String appId;
    /**
     * 私钥
     */
    private String secret;
    /**
     * 获取的accessToken
     */
    private String accessToken;
    /**
     * 过期时间  存的2小时后得毫秒值
     */
    private Long expiresTime;

    public AccessTokenBean(String appId, String secret, String accessToken, Long expiresTime) {
        this.appId = appId;
        this.secret = secret;
        this.accessToken = accessToken;
        if(expiresTime>0){
            this.expiresTime = System.currentTimeMillis()+expiresTime*1000;
        }
    }

    /**
     * 是否过期
     * @return
     */
    public boolean isExpired(){
        return System.currentTimeMillis() > expiresTime;
    }

    /**
     * 构建获取access_token 请求对象
     * @param appId 用户唯一标识
     * @param secert 用户唯一标识密钥
     * @return {@link HashMap}
     */
    public static HashMap<String,String> requestOf(String appId,String secert){
        HashMap<String,String> request = new HashMap<String,String>();
        request.put("appId",appId);
        request.put("secret",secert);
        return  request;
    }

    /**
     * 构建access_token对象
     * @param accessToken token
     * @param expiresTime 过期时间
     * @return {@link AccessTokenBean}
     */
    public static AccessTokenBean responseOf(String accessToken,long expiresTime){
        return new AccessTokenBean("","",accessToken,expiresTime);
    }


}
